package com.akk.platform.manage;

import com.akk.module.avs.entity.AvsHotBank;
import com.akk.module.avs.enums.AvsHotBankEnums;
import com.akk.module.avs.model.request.AvsHotBankRequest;
import com.akk.module.avs.service.AvsHotBankService;
import com.akk.module.upload.service.PictureService;
import com.akk.utils.PageUtils;
import com.akk.utils.Query;
import com.akk.utils.R;
import com.akk.utils.RestResponse;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/platform/manage/hotBank/")
public class HotBankAction {

    @Autowired
    private AvsHotBankService hotBankService;

    @Autowired
    private PictureService pictureServices;

    @RequestMapping(value = "/list")
    @RequiresPermissions("sys:hotBank:list")
    public RestResponse lis(@Valid Query query,BindingResult bindingResult){
        if (bindingResult.hasErrors())
            return RestResponse.build("1",bindingResult.getFieldError().getDefaultMessage(),null);
        Map param = query.getSearch();
        PageHelper.startPage(query.getPage(),query.getLimit());
        List<AvsHotBank> list = hotBankService.queryList();
        return RestResponse.build(PageUtils.buildPage(list,(int) new PageInfo<>(list).getTotal(),query));
    }

    @RequestMapping(value = "/save",method = RequestMethod.POST)
    @RequiresPermissions("sys:hotBank:save")
    public RestResponse save(@Valid AvsHotBankRequest request, BindingResult bindingResult){
        if (bindingResult.hasErrors())
            return RestResponse.build("1",bindingResult.getFieldError().getDefaultMessage());
        AvsHotBank hotBankEntity = new AvsHotBank();
        if (request.getBankIcon() == null){
            return RestResponse.build("1","请上传文件");
        }else {
            Map<String,Object> result = pictureServices.uploadPicture(request.getBankIcon());
            if (result.get("status").toString().equals("1")){
                return RestResponse.build("1","文件上传失败");
            }
            hotBankEntity.setBankIcon(result.get("url").toString());
        }
        hotBankEntity.setBankHot(request.getBankHot());
        hotBankEntity.setBankName(request.getBankName());
        hotBankEntity.setBankUrl(StringEscapeUtils.unescapeHtml(request.getBankUrl()));
        hotBankService.save(hotBankEntity);
        return RestResponse.build("success");
    }

    @RequestMapping(value = "/info/{id}")
    @RequiresPermissions("sys:hotBank:info")
    public RestResponse info(@PathVariable Long id){
        AvsHotBank hotBankEntity = hotBankService.queryById(id);
        if (hotBankEntity == null)
            return RestResponse.build("1","信息不存在");
        return RestResponse.build(hotBankEntity);
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @RequiresPermissions("sys:hotBank:update")
    public RestResponse update(@Valid AvsHotBankRequest request, BindingResult bindingResult){
        if (bindingResult.hasErrors())
            return RestResponse.build("1",bindingResult.getFieldError().getDefaultMessage());
        if(request.getBankId() == null || request.getBankId() == 0){
            return RestResponse.build("1","参数非法");
        }
        AvsHotBank entity = hotBankService.queryById(request.getBankId());
        if (entity == null){
            return RestResponse.build("1","信息不存在");
        }
        entity.setBankName(request.getBankName());
        if (request.getBankIcon() != null){
            Map<String,Object> result = pictureServices.uploadPicture(request.getBankIcon());
            if (result.get("status").toString().equals("1")){
                return RestResponse.build("1","文件上传失败");
            }
            entity.setBankIcon(result.get("url").toString());
        }
        entity.setBankHot(request.getBankHot());
        entity.setBankUrl(StringEscapeUtils.unescapeHtml(request.getBankUrl()));
        hotBankService.update(entity);
        return RestResponse.build("success");
    }

    @RequestMapping(value = "/addHot/{id}")
    @RequiresPermissions("sys:hotBank:addHot")
    public RestResponse addHot(@PathVariable Long id){
        AvsHotBank hotBankEntity = hotBankService.queryById(id);
        if (hotBankEntity == null)
            return RestResponse.build("1","信息不存在");
        else if (hotBankEntity.getBankHot() == AvsHotBankEnums.HotType.HOT.getValue())
            return RestResponse.build("1","已经是热门推荐了");
        hotBankEntity.setBankHot(AvsHotBankEnums.HotType.HOT.getValue());
        hotBankService.update(hotBankEntity);
        return RestResponse.build("success");
    }

    @RequestMapping(value = "/removeHot/{id}")
    @RequiresPermissions("sys:hotBank:removeHot")
    public RestResponse removeHot(@PathVariable Long id){
        AvsHotBank hotBankEntity = hotBankService.queryById(id);
        if (hotBankEntity == null)
            return RestResponse.build("1","信息不存在");
        else if (hotBankEntity.getBankHot() == AvsHotBankEnums.HotType.NONE.getValue())
            return RestResponse.build("1","此数据不在热门推荐中");
        hotBankEntity.setBankHot(AvsHotBankEnums.HotType.NONE.getValue());
        hotBankService.update(hotBankEntity);
        return RestResponse.build("success");
    }

    @RequestMapping(value = "/delete/{id}")
    @RequiresPermissions("sys:hotBank:delete")
    public RestResponse delete(@PathVariable Long id){
        hotBankService.delete(id);
        return RestResponse.build("success");
    }

    public static void main(String args[]){

        String sql="1' or '1'='1";
        System.out.println("防SQL注入:"+StringEscapeUtils.escapeSql(sql)); //防SQL注入

        System.out.println("转义HTML,注意汉字:"+StringEscapeUtils.escapeHtml("<font>chen磊  xing</font>"));    //转义HTML,注意汉字
        System.out.println("反转义HTML:"+StringEscapeUtils.unescapeHtml("&lt;font&gt;chen&#30922; &amp;  xing&lt;/font&gt;"));  //反转义HTML

        System.out.println("转成Unicode编码："+StringEscapeUtils.escapeJava("陈磊兴"));     //转义成Unicode编码

        System.out.println("转义XML："+StringEscapeUtils.escapeXml("<name>陈磊兴</name>"));   //转义xml
        System.out.println("反转义XML："+StringEscapeUtils.unescapeXml("<name>陈磊兴</name>"));    //转义xml

    }

}
